﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace NaviCar.Models
{
    public class GerenciadorModelo : Framework.Comum.DAL.IGerenciadorContexto
    {        
        private const string CHAVE_SESSAO = "___DADOS_";

        public void DefinirInstancia(object repositorio)
        {
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session[CHAVE_SESSAO] = repositorio;
            }
        }


        public object RecuperarInstancia()
        {
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                return HttpContext.Current.Session[CHAVE_SESSAO];
            }
            else return null;
        }

        public void LiberarInstacia()
        {
            if (HttpContext.Current.Session != null)
            {
                if(HttpContext.Current.Session[CHAVE_SESSAO] != null)
                {
                    ((IDisposable)HttpContext.Current.Session[CHAVE_SESSAO]).Dispose();
                }

                HttpContext.Current.Session[CHAVE_SESSAO] = null;
            }
            
        }
        
    }
}